﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LeagueOfRila.Model;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using LeagueOfRila.Callback;

namespace LeagueOfRila.Controller
{
    public class GameController : BaseController
    {
        private KeyboardState previousState;
        private double songDuration;
        private float trackTimer;
        

        public override string viewName
        {
            get { return "LeagueOfRila.View.GameView"; }
            set { }
        }
        public override string modelName
        {
            get { return "LeagueOfRila.Model.GameModel"; }
            set { }
        }

        public GameController() {
        }

        public GameModel GetModel()
        {
      
            return this.model as GameModel;
        }


        public override void Update(GameTime gameTime)
        {

            trackTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            //Musique
            int nbSong = Client.mapSongsList.Count();
            if (nbSong > 0)
            {
                //test si la durée de la musique jouée est dépassée
                if (Client.playingSong > -1)
                {
                    if (songDuration < trackTimer)
                    {
                        Client.StopMusic();
                        trackTimer = 0;
                    }
                }

                if (!Client.musicLance)
                {
                    if (Client.playingSong < nbSong-1 )
                    {
                        Client.playingSong++;
                    }
                    else
                    {
                        Client.playingSong = 0;
                    }

                    Client.PlayMusic(Client.mapSongsList[Client.playingSong], false);
                    songDuration = Client.mapSongsList[Client.playingSong].Duration.TotalSeconds;
                }

            }
            
            //Déplacements
            KeyboardState currentState = Keyboard.GetState();

            if (previousState == null)
                previousState = currentState;
            

            base.Update(gameTime);

            if (currentState.GetPressedKeys().Contains(Keys.S) && currentState.GetPressedKeys().Contains(Keys.Q))
            {
                this.GetModel().GoDown();
                this.GetModel().GoLeft();
                this.GetModel().GoLeft();
                this.GetModel().addStep();
                this.GetModel().SetOrientation(1);
                //this.GetModel().tellServerCharInfos();
            }

            else if (currentState.GetPressedKeys().Contains(Keys.S) && currentState.GetPressedKeys().Contains(Keys.D))
            {
                this.GetModel().GoDown();
                this.GetModel().GoRight();
                this.GetModel().GoRight();
                this.GetModel().addStep();
                this.GetModel().SetOrientation(2);
                //this.GetModel().tellServerCharInfos();
            }

            else if (currentState.GetPressedKeys().Contains(Keys.Z) && currentState.GetPressedKeys().Contains(Keys.D))
            {
                this.GetModel().GoUp();
                this.GetModel().GoRight();
                this.GetModel().GoRight();
                this.GetModel().addStep();
                this.GetModel().SetOrientation(2);
                //this.GetModel().tellServerCharInfos();
            }

            else if (currentState.GetPressedKeys().Contains(Keys.Z) && currentState.GetPressedKeys().Contains(Keys.Q))
            {
                this.GetModel().GoUp();
                this.GetModel().GoLeft();
                this.GetModel().GoLeft();
                this.GetModel().addStep();
                this.GetModel().SetOrientation(3);
                //this.GetModel().tellServerCharInfos();
            }

            else if (currentState.GetPressedKeys().Contains(Keys.S))
            {
                this.GetModel().GoDown();
                this.GetModel().GoDown();
                this.GetModel().addStep();
                this.GetModel().SetOrientation(0);
                //this.GetModel().tellServerCharInfos();
            }
            else if (currentState.GetPressedKeys().Contains(Keys.Z))
            {
                this.GetModel().GoUp();
                this.GetModel().GoUp();
                this.GetModel().addStep();
                this.GetModel().SetOrientation(3);
                //this.GetModel().tellServerCharInfos();
            }
            else if (currentState.GetPressedKeys().Contains(Keys.Q))
            {
                this.GetModel().GoLeft();
                this.GetModel().GoLeft();
                this.GetModel().addStep();
                this.GetModel().SetOrientation(1);
                //this.GetModel().tellServerCharInfos();
            }
            else if (currentState.GetPressedKeys().Contains(Keys.D))
            {
                this.GetModel().GoRight();
                this.GetModel().GoRight();
                this.GetModel().addStep();
                this.GetModel().SetOrientation(2);
                //this.GetModel().tellServerCharInfos();
            }
            else if (currentState.GetPressedKeys().Contains(Keys.Tab))
            {
                if(currentState != previousState)
                  this.GetModel().GetStats();
                //this.GetModel().tellServerCharInfos();
            }

            //touches attaques

           
    

            List<Keys> listprevious = previousState.GetPressedKeys().ToList();
            foreach (Keys key in listprevious)
            {
                if (!currentState.GetPressedKeys().Contains(key))
                {
                    if (key.ToString() == Keys.Up.ToString())
                    {
                        this.GetModel().attackUp();
                    }
                    else if (key.ToString() == Keys.Down.ToString())
                    {
                        this.GetModel().attackDown();
                    }
                }
            }

            previousState = currentState;

            this.GetModel().updateCamera();

        }
    }
}
